qui(
    function (require, exports, module) {

        Record.Signal_Dao = $.extend({}, EpgWeb.Dao, {
            addRecordSignal:function(sendData,handler){
                this.sendAjax(Record.urlConfig.doSignalSaveUrl,"post",sendData,handler,"/record/signal/save");
            },
            deleteRecordSignal:function(sendData,handler){
                this.sendAjax(Record.urlConfig.doSignalDelUrl,"post",sendData,handler,"/record/signal/del");
            },
            editRecordSignal:function(sendData,handler){
                this.sendAjax(Record.urlConfig.doSignalEditUrl,"post",sendData,handler,"/record/signal/edit");
            },
            getRecordSignal:function(entityId,handler){
                this.sendAjax(Record.urlConfig.doSignalGetByIdUrl,"post",{id:entityId},handler,"/record/signal/getById");
            }
        });

        Record.Signal_Grid = function (options) {
            this.dao = Record.Signal_Dao;
            this.config = Record.Signal_Config;
            this.util = Record.Signal_Util;
            this.options = options || {};
        };

        Com.inherits(Record.Signal_Grid, Ancestor.Grid).extend({
            doInit: function () {
                var self = this;
                EpgWeb.ajaxQueue.afterLoadComplete(function () {
                    self.bindEvent();
                    self.initGrid();
                    self.initDlg();
                    self.initSuggest();
                })
                EpgWeb.ajaxQueue.startLoad();
            },
            bindEvent: function () {
                var self = this;

                //创建请求
                var $sendAddButton = $("button[name=add_signal_btn]");
                $sendAddButton.bind("click", function (event) {
                    Record.Signal_Util.sendAddRecordSignal();
                });

                //修改请求
                var $sendEditButton = $("button[name=edit_signal_btn]");
                $sendEditButton.bind("click", function (event) {
                    Record.Signal_Util.sendEditRecordSignal();
                });

                //新建
                $("button[name=open_new_dlg_btn]").bind("click", function (event) {
                    $sendAddButton.show();
                    $sendEditButton.hide();
                    $("form[role=addForm]").dialog("open");
                    $("form[role=addForm]")[0].reset();
                });

                //取消
                $("button[name=cancel_btn]").bind("click", function (event) {
                    $("form[role=addForm]").dialog("close")
                });

                //搜索
                $("button[name=query_btn]").bind("click", function (event) {
                    self.query();
                })


            },

            initGrid: function (options) {
                var self = this;
                self.$grid = self.options.$grid;
                var params = self.getParam();
                var gridConfig = self.getGridConfig(self.config.SIGNAL_DATA_LIST);
                options = $.extend(true, {
                    mtype: 'post',
                    postData: $.extend(true, {}, params)
                }, gridConfig, options);
                self.$grid.jqGrid(options);

                //简单响应式
                $(window).resize(function(){
                    self.$grid.setGridWidth($(window).width()*0.9);
                });


            },
            query: function (callback) {
                var self = this;
                var $grid = self.$grid;
                var params = self.getParam();
                params.sortType = "desc";
                $grid.jqGrid('setGridParam', {
                    page: 1,
                    datatype: "json",
                    postData: params
                }).trigger('reloadGrid');
            },
            getParam: function () {
                var params = {};
                var inputArr = $('form[role="queryForm"]').find("input[role=query-text]")
                $.each(inputArr, function (i, element) {
                    if (element.value.trim().length > 0) {
                        params [$(element).attr("name")] = element.value.trim()
                    }
                })

                console.log("query params:" + JSON.stringify(params));
                return { paramsObj: JSON.stringify(params) };
            },
            initDlg: function () {
                $("form[role=addForm]").dialog({
                    title: "新建信号",
                    autoOpen: false,
                    height: 640,
                    width: 840,
                    modal: true
                })
            },
            initSuggest: function () {
                var ClusterSuggest = {
                    options: {
                        ajaxSettings: {
                            beforeSend: function (request) {
                                var authBase = Ancestor.Util.getAuth("/sginal/server/clusterList");
                                request.setRequestHeader('Sign', authBase.sign);
                                request.setRequestHeader("Time", authBase.clientTime);
                                request.setRequestHeader("From", authBase.from);
                                request.setRequestHeader("UserName", (auth.baseInfo.authInfo == null) ? "" : escape(auth.baseInfo.authInfo.userName).toLocaleLowerCase().replace(/%u/gi, '\\u'));
                            }
                        },
                        select: function (event, object) {
                            var obj = { "clusterName": Number(object.item.id) };
                            return true;
                        },
                        source: "/api/sginal/server/clusterList",
                        method: "post",
                        autoFilter: false,
                        autoFocus: false,
                        multiple: false,
                        parseValue: function () {
                            var val = this.element.val(),
                                ret = []
                            return ret;
                        },
                        getParam: function (term) {
                            return { "clusterName": $.trim(term) }
                        },
                        process: function (json) {
                            var ret = [],
                                self = this,
                                options = self.options,
                                itemData = null;
                            var data = null;
                            if (json == null) {
                                return [];
                            }
                            if (json.code === "A00000") {
                                data = json.data == null ? null : json.data;
                            }
                            if (!data) {
                                return [];
                            }
                            $.map(data.result, function (item) {
                                itemData = {};
                                itemData[options.valueKey] = item["id"];
                                itemData[options.labelKey] = item["name"]
                                ret.push(itemData);
                            });
                            return ret;
                        },
                        template: function (item) {
                            return item.label;
                        }
                    }
                }
                $("input[name=server_cluster_name]").suggest(ClusterSuggest.options);
            }
        });

    }
);